/***************************************************************
 * Name:      mywxcalcforfatherMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    my3world (my3world@gmail.com)
 * Created:   2013-09-23
 * Copyright: my3world ()
 * License:
 **************************************************************/

#include "mywxcalcforfatherMain.h"
#include "sys_clipboard.h"
#include "str_operation.hpp"
#include <wx/msgdlg.h>
#include <map>
#include <vector>

//(*InternalHeaders(mywxcalcforfatherDialog)
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

const int START_POSITION = 1, END_POSITION = 50;
std::string arr_search[50] = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
                       "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
                       "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
                       "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
                       "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
                      };

//(*IdInit(mywxcalcforfatherDialog)
const long mywxcalcforfatherDialog::ID_TEXTCTRL1 = wxNewId();
const long mywxcalcforfatherDialog::ID_TEXTCTRL2 = wxNewId();
const long mywxcalcforfatherDialog::ID_BUTTON3 = wxNewId();
const long mywxcalcforfatherDialog::ID_BUTTON1 = wxNewId();
const long mywxcalcforfatherDialog::ID_BUTTON2 = wxNewId();
//*)

BEGIN_EVENT_TABLE(mywxcalcforfatherDialog,wxDialog)
    //(*EventTable(mywxcalcforfatherDialog)
    //*)
END_EVENT_TABLE()

mywxcalcforfatherDialog::mywxcalcforfatherDialog(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(mywxcalcforfatherDialog)
    wxBoxSizer* BoxSizer3;

    Create(parent, wxID_ANY, _("码数统计器"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
    wxFont thisFont(12,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    SetFont(thisFont);
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    BoxSizer3 = new wxBoxSizer(wxVERTICAL);
    TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxSize(369,192), wxTE_MULTILINE|wxVSCROLL|wxHSCROLL, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    wxFont TextCtrl1Font(12,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    TextCtrl1->SetFont(TextCtrl1Font);
    BoxSizer3->Add(TextCtrl1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    TextCtrl2 = new wxTextCtrl(this, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxSize(433,175), wxTE_MULTILINE|wxVSCROLL|wxHSCROLL, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    BoxSizer3->Add(TextCtrl2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    ButtonClear = new wxButton(this, ID_BUTTON3, _("清除"), wxDefaultPosition, wxSize(139,38), 0, wxDefaultValidator, _T("ID_BUTTON3"));
    BoxSizer2->Add(ButtonClear, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    ButtonAppend = new wxButton(this, ID_BUTTON1, _("添加"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer2->Add(ButtonAppend, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    ButtonStart = new wxButton(this, ID_BUTTON2, _("统计"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
    BoxSizer2->Add(ButtonStart, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    BoxSizer3->Add(BoxSizer2, 0, wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    BoxSizer1->Add(BoxSizer3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    SetSizer(BoxSizer1);
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);

    Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&mywxcalcforfatherDialog::OnButtonClearClick);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&mywxcalcforfatherDialog::OnButtonAppendClick);
    Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&mywxcalcforfatherDialog::OnButtonStartClick);
    //*)
}

mywxcalcforfatherDialog::~mywxcalcforfatherDialog()
{
    //(*Destroy(mywxcalcforfatherDialog)
    //*)
}

void mywxcalcforfatherDialog::OnButtonStartClick(wxCommandEvent& event)
{
    std::string str_memory = std::string(TextCtrl1->GetValue().mbc_str());
    std::string str_result;

    replace_split_with_blank(str_memory, ":,.\n;");
    replace_some_words(str_memory);

    int times[50] = {0,0,0,0,0,0,0,0,0,0,
                     0,0,0,0,0,0,0,0,0,0,
                     0,0,0,0,0,0,0,0,0,0,
                     0,0,0,0,0,0,0,0,0,0,
                     0,0,0,0,0,0,0,0,0,0};

    for (int i = START_POSITION; i != END_POSITION; i++)
        times[i] = total_appear_times(str_memory, arr_search[i]);

    std::map<int, std::vector<std::string> > m;
    for (int i = START_POSITION; i != END_POSITION; i++)
        m[times[i]].push_back(arr_search[i]);

    char arr[10];
    for (std::map<int, std::vector<std::string> >::const_iterator it = m.begin(); it != m.end(); ++it)
    {
        str_result += "[" + std::string(itoa(it->first, arr, 10)) + "]:";
        for (std::vector<std::string>::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
            str_result += " " + *it2;
        str_result += "\n";
    }
    str_result += "\n";

    TextCtrl1->Clear();
    TextCtrl2->Clear();
    TextCtrl2->AppendText(wxString(str_result.c_str(), wxConvUTF8));

    copy_text_to_clipboard(str_result.c_str());
}


void mywxcalcforfatherDialog::OnButtonClearClick(wxCommandEvent& event)
{
    TextCtrl1->Clear();
    TextCtrl2->Clear();
}

void mywxcalcforfatherDialog::OnButtonAppendClick(wxCommandEvent& event)
{
    TextCtrl1->AppendText(wxString(get_text_from_clipboard(), wxConvUTF8));
}
